#!/bin/bash
# Minecraft multiple instaces loader. ver 0.1
#
# Copyright 2012 Pavel A. Terentyev <pavel@terentyev.info>
#
# Licensed  under the Apache License, Version 2.0 (the “License”); you may not use
# this  file  except  in compliance with the License. You may obtain a copy of the
# License at
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless  required by applicable law or agreed to in writing, software distributed
# under  the  License  is  distributed  on an “AS IS” BASIS, WITHOUT WARRANTIES OR
# CONDITIONS  OF  ANY  KIND,  either  express  or implied. See the License for the
# specific language governing permissions and limitations under the License.
#
# Example folders tree
#+-$GAMEDIR/
#  +-Vanilla/
#  | +-.minecraft/
#  |   +-bin/
#  |   +-minecraft.jar
#  +-mc_1.3.1/
#  | +-.minecraft/
#  |   +-bin/
#  |   +-minecraft.jar
#  |   +-mcpatcher.jar
#  +-mc_1.3.2/
#    +-.minecraft/
#      +-bin/
#      +-minecraft.jar
#      +-mcpatcher.jar

# directory name, where you want to store Minecraft versions
GAMEDIR=${HOME}/.mcdata
MINECRAFT_DLOAD_URI="https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft.jar"

installmenu(){
while :
do
    $DIALOG --backtitle 'Minecraft loader by Sanovskiy' --inputbox \
        "Give a name to a new installation. It must be valid filename for your filesystem." 0 0 2> /tmp/inputbox.tmp.$$
    retval=$?
    input=`cat /tmp/inputbox.tmp.$$`
    rm -f /tmp/inputbox.tmp.$$
    case $retval in
        0)
            if [ -z "$input" ]; then
                $DIALOG --backtitle 'Minecraft loader by Sanovskiy' --title "Error" --msgbox "Can't use empty filename." 5 45
                continue
            fi
            echo "'$input'"
            ;;
        1)
            return
            ;;
    esac
    if [ -d $GAMEDIR/$input ]; then
        $DIALOG --backtitle 'Minecraft loader by Sanovskiy' --title "Error" --msgbox "Name already exists." 5 45
        continue
    fi
    mkdir $GAMEDIR/$input
    if [ ! -d $GAMEDIR/$input ]; then
        $DIALOG --backtitle 'Minecraft loader by Sanovskiy' --title "Error" --msgbox "You supplied bad filename." 5 45
        continue
    fi
    mkdir $GAMEDIR/$input/.minecraft
    break
done
wget $MINECRAFT_DLOAD_URI -O $GAMEDIR/$input/.minecraft/minecraft.jar 

}

mainmenu(){
  cmd=($DIALOG --backtitle 'Minecraft loader by Sanovskiy' --menu "Select game:" 0 0 0)
  OPTIONS=()
  ANSWERS=()
  NUM=0
  VERSIONS=`ls -1 -d $GAMEDIR/*/ | tr '\n' '\0' | xargs -0 -n 1 basename`
  for f in $VERSIONS;do
    OPTIONS=("${OPTIONS[@]}" $NUM $f)
    ANSWERS[$NUM]=$f
    NUM=$(($NUM+1))
  done;
  OPTIONS=("${OPTIONS[@]}" "+" "Install new version")
  CHOICES=$("${cmd[@]}" "${OPTIONS[@]}" 2>&1 >/dev/tty)
  echo $CHOICES 
  if [ -z $CHOICES ]; then
    echo Exiting...
    exit 0
  fi
  if [ "$CHOICES" == "+" ]; then
      installmenu
  else
      GameName=${ANSWERS[$CHOICES]}
      if [ ! -f $GAMEDIR/$GameName/.minecraft/mcpatcher.jar ]; then
        echo Launching Minecraft
        java -Xmx1024M -Xms512M -Duser.home=$GAMEDIR/$GameName -cp \
            $GAMEDIR/$GameName/.minecraft/minecraft.jar net.minecraft.LauncherFrame
      else
        secondmenu
      fi
      
  fi
}

secondmenu(){
  cmd=($DIALOG --backtitle 'Minecraft loader by Sanovskiy' --menu "Select application to run under ${GameName}:" 0 0 0)
  OPTIONS=(m Minecraft p MCPatcher)
  while :
  do
    CHOICES=$("${cmd[@]}" "${OPTIONS[@]}" 2>&1 >/dev/tty)
    if [ ! -z $CHOICES ]; then
      case "$CHOICES" in
        m )
          echo Launching Minecraft
          java -Xmx1024M -Xms512M -Duser.home=$GAMEDIR/$GameName -cp \
            $GAMEDIR/$GameName/.minecraft/minecraft.jar \
            net.minecraft.LauncherFrame
        ;;
        p )
          echo Launching Minecraft patcher
          java -Xmx1024M -Xms512M -Duser.home=$GAMEDIR/$GameName \
            -jar $GAMEDIR/$GameName/.minecraft/mcpatcher.jar
        ;;
        *   ) echo "Bad choice";;
      esac
    else
      break
    fi
  done
}


if [ ! -d "$GAMEDIR" ]; then
  mkdir $GAMEDIR
  if [ ! -d "$GAMEDIR" ]; then
    echo "Error: Can't create data directory."
    exit 0
  fi
fi

DIALOG=${DIALOG=dialog}
#DIALOG=${DIALOG=Xdialog}

VERSIONS_COUNT=`ls -d $GAMEDIR/*/ | wc -l`
if [ $VERSIONS_COUNT = 0 ]; then
  if [ -d "$HOME/.minecraft" ]; then
    $DIALOG --backtitle 'Minecraft loader by Sanovskiy' --title " Import" \
        --yesno "There is some minecraft version installed.\nDo you want to to import it?" 0 0
    case $? in
        0)
            # Yes
            $DIALOG --backtitle 'Minecraft loader by Sanovskiy' --title " Import" \
                --infobox "Please wait.\nImport in progress." 4 35
            mkdir $GAMEDIR/MyCraft/
            cp --force --recursive $HOME/.minecraft $GAMEDIR/MyCraft/ 
            ;;
        1)
            # No
            ;;
        255)
            # ESC
            exit 0
            ;;
    esac
  fi
  VERSIONS_COUNT=`ls -d $GAMEDIR/*/ | wc -l`
  if [ $VERSIONS_COUNT = 0 ]; then
    $DIALOG --backtitle 'Minecraft loader by Sanovskiy' --title " Install Minecraft" \
        --yesno "Do you want install fresh version of Minecraft?" 0 0
    case $? in
        0)
            # Yes
            installmenu
            ;;
        1)
            # No
            ;;
        255)
            # ESC
            exit 0
            ;;
    esac
  fi
  VERSIONS_COUNT=`ls -d $GAMEDIR/*/ | wc -l`
fi

if [ $VERSIONS_COUNT = 0 ]; then
  $DIALOG --backtitle 'Minecraft loader by Sanovskiy' --title "Error"  --msgbox 'There is no Minecraft versions installed.' 5 45
  exit 0
fi

while :
do
  mainmenu
done
exit 0
